package com.qyer.search.model.mutable;

import org.apache.commons.collections4.MapUtils;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by tangzheng on 16/9/1.
 */
public class PreferDestination {

  private TreeMap<Integer, Double> countryPrefer;
  private TreeMap<Integer, Double> cityPrefer;
  private boolean isEmpty;

  public PreferDestination(TreeMap<Integer, Double> coutryPrefer,
                           TreeMap<Integer, Double> cityPrefer) {
    this.countryPrefer = coutryPrefer;
    this.cityPrefer = cityPrefer;
    this.isEmpty = false;
  }

  public PreferDestination() {
    this.isEmpty = true;
  }

  public TreeMap<Integer, Double> getCountryPrefer() {
    return countryPrefer;
  }

  public void setCountryPrefer(TreeMap<Integer, Double> countryPrefer) {
    this.countryPrefer = countryPrefer;
  }

  public boolean isEmpty() {
    return isEmpty;
  }

  public void setIsEmpty(boolean isEmpty) {
    this.isEmpty = isEmpty;
  }

  public final TreeMap<Integer, Double> getCoutryPrefer() {
    return countryPrefer;
  }

  public final void setCoutryPrefer(TreeMap<Integer, Double> coutryPrefer) {
    this.countryPrefer = coutryPrefer;
  }

  public final TreeMap<Integer, Double> getCityPrefer() {
    return cityPrefer;
  }

  public final void setCityPrefer(TreeMap<Integer, Double> cityPrefer) {
    this.cityPrefer = cityPrefer;
  }

  public final void addCountryPrefer(int countryId, double weight){
    if(this.countryPrefer == null){
      this.countryPrefer = new TreeMap<>(new Comparator<Integer>() {
        @Override public int compare(Integer o1, Integer o2) {
          return (o2 - o1);
        }
      });
      this.countryPrefer.put(countryId, weight);
    }else {
      if(this.countryPrefer.containsKey(countryId)){
        double t_w = this.countryPrefer.get(countryId);
        if(weight > t_w)
          this.countryPrefer.put(countryId, weight);
      }else {
        this.countryPrefer.put(countryId, weight);
      }
    }
  }

  public final void addCityPrefer(int cityId, double weight){
    if(this.cityPrefer == null){
      this.cityPrefer = new TreeMap<>(new Comparator<Integer>() {
        @Override public int compare(Integer o1, Integer o2) {
          return (o2 - o1);
        }
      });
      this.cityPrefer.put(cityId, weight);
    }else {
      if(this.cityPrefer.containsKey(cityId)){
        double t_w = this.cityPrefer.get(cityId);
        if(weight > t_w)
          this.cityPrefer.put(cityId, weight);
      }else {
        this.cityPrefer.put(cityId, weight);
      }
    }
  }

  public final void addPlacePrefer(int placeId, int type, double weight){
    if(type == 1){
      this.addCountryPrefer(placeId, weight);
      this.isEmpty = false;
    }else if(type == 2){
      this.addCityPrefer(placeId, weight);
      this.isEmpty = false;
    }
  }

  @Override public boolean equals(Object o) {
    if (this == o)
      return true;
    if (o == null || getClass() != o.getClass())
      return false;

    PreferDestination that = (PreferDestination) o;

    if (!countryPrefer.equals(that.countryPrefer))
      return false;
    return cityPrefer.equals(that.cityPrefer);

  }

  @Override public int hashCode() {
    int result = countryPrefer.hashCode();
    result = 31 * result + cityPrefer.hashCode();
    return result;
  }

  public final boolean isPreferPlace(int placeId, int type){
    if(type == 1){
      if(this.countryPrefer == null){
        return false;
      }else {
        return this.countryPrefer.containsKey(placeId);
      }
    }else if(type == 2){
      if(this.cityPrefer == null){
        return false;
      }else {
        return this.cityPrefer.containsKey(placeId);
      }
    }
    return false;
  }

  @Override public String toString() {
    return "PreferDestination{" +
      "countryPrefer=" + countryPrefer +
      ", cityPrefer=" + cityPrefer +
      '}';
  }
}
